<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.1.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/all.min.css" integrity="sha256-DfWjNxDkM94fVBWx1H5BMMp0Zq7luBlV8QRcSES7s+0=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.11.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="Duilib-自动换行的处理Duilib中自动没有自动换行的属性，如果需要实现自动换行，就需要在代码中计算总字体的长度，根据需要添加‘\n’换行符进行换行。">
<meta property="og:type" content="article">
<meta property="og:title" content="duilib 控件文字自动换行">
<meta property="og:url" content="http://example.com/2020/01/10/Duilib-%E8%87%AA%E5%8A%A8%E6%8D%A2%E8%A1%8C/index.html">
<meta property="og:site_name" content="Youan">
<meta property="og:description" content="Duilib-自动换行的处理Duilib中自动没有自动换行的属性，如果需要实现自动换行，就需要在代码中计算总字体的长度，根据需要添加‘\n’换行符进行换行。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-01-09T16:00:00.000Z">
<meta property="article:modified_time" content="2023-01-04T12:18:12.368Z">
<meta property="article:author" content="lee">
<meta property="article:tag" content="duilib">
<meta property="article:tag" content="控件文字换行">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2020/01/10/Duilib-%E8%87%AA%E5%8A%A8%E6%8D%A2%E8%A1%8C/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2020/01/10/Duilib-%E8%87%AA%E5%8A%A8%E6%8D%A2%E8%A1%8C/","path":"2020/01/10/Duilib-自动换行/","title":"duilib 控件文字自动换行"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>duilib 控件文字自动换行 | Youan</title>
  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Youan</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">好记性不如烂笔头</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Duilib-%E8%87%AA%E5%8A%A8%E6%8D%A2%E8%A1%8C%E7%9A%84%E5%A4%84%E7%90%86"><span class="nav-number">1.</span> <span class="nav-text">Duilib-自动换行的处理</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BE%8B%E5%AD%90-%E2%80%93-%E5%BC%B9%E6%A1%86%E6%8F%90%E7%A4%BA%E4%B8%AD%E7%9A%84%E6%96%87%E6%9C%AC%EF%BC%8C%E8%87%AA%E5%8A%A8%E6%8D%A2%E8%A1%8C%E7%9A%84%E5%A4%84%E7%90%86"><span class="nav-number">1.1.</span> <span class="nav-text">例子 – 弹框提示中的文本，自动换行的处理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%B7%E4%BD%93%E5%AE%9E%E7%8E%B0%E8%AE%A1%E7%AE%97%E6%96%87%E6%9C%AC%E9%95%BF%E5%BA%A6%EF%BC%8C%E5%B9%B6%E6%8D%A2%E8%A1%8C%E6%98%BE%E7%A4%BA"><span class="nav-number">1.1.1.</span> <span class="nav-text">具体实现计算文本长度，并换行显示</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9C%A8%E5%AF%B9%E6%96%87%E6%9C%AC%E8%BF%9B%E8%A1%8C%E6%8D%A2%E8%A1%8C%E6%98%BE%E7%A4%BA%E7%9A%84%E6%97%B6%E5%80%99%EF%BC%8C%E6%9C%89%E4%B8%80%E7%82%B9%E7%89%B9%E5%88%AB%E9%9C%80%E8%A6%81%E6%B3%A8%E6%84%8F%EF%BC%8C%E5%B0%B1%E6%98%AF%E5%9C%A8%E6%98%BE%E7%A4%BA%E7%AA%97%E4%BD%93%E7%9A%84xml%E6%96%87%E4%BB%B6%E8%AE%BE%E7%BD%AE%E4%B8%AD%EF%BC%8C%E9%9C%80%E8%A6%81%E5%B0%86%E8%AE%BE%E7%BD%AE%E4%B8%BA%E9%80%8F%E6%98%8E%E5%B1%9E%E6%80%A7%EF%BC%8C%E5%8D%B3-bktrans-x3D-%E2%80%9Dtrue%E2%80%9D%EF%BC%8C%E8%BF%99%E4%B8%80%E5%B1%9E%E6%80%A7%E5%9C%A8Duilib%E4%B8%AD%E9%BB%98%E8%AE%A4%E6%98%AFfalse%E7%9A%84%EF%BC%8C%E6%89%80%E4%BB%A5%E5%BF%85%E9%A1%BB%E5%9C%A8xml%E4%B8%AD%E6%98%BE%E7%A4%BA%E7%9A%84%E8%AE%BE%E7%BD%AE%E8%AF%A5%E5%B1%9E%E6%80%A7%E3%80%82%E5%A6%82%E6%9E%9C-bktrans-x3D-%E2%80%9Dfalse%E2%80%9D%EF%BC%8C%E9%82%A3%E4%B9%88Label%E4%B8%AD%E7%9A%84%E6%96%87%E6%9C%AC%E5%B0%B1%E4%B8%8D%E4%BC%9A%E6%8D%A2%E8%A1%8C%E6%98%BE%E7%A4%BA%EF%BC%8C%E8%80%8C%E6%98%AF%E5%8F%AA%E6%98%BE%E7%A4%BA%E4%B8%80%E8%A1%8C%E3%80%82%E8%BF%99%E4%B8%AA%E9%97%AE%E9%A2%98%E7%9A%84%E5%85%B7%E4%BD%93%E5%8E%9F%E5%9B%A0%E4%B8%8D%E5%A4%AA%E6%B8%85%E6%A5%9A%EF%BC%8C%E6%8E%A8%E6%B5%8B-bktrans-%E5%B1%9E%E6%80%A7%E7%9A%84%E8%AE%BE%E7%BD%AE%E4%BC%9A%E6%9B%B4%E6%94%B9%E7%BB%98%E5%9B%BE%E5%BC%95%E6%93%8E%E8%B0%83%E7%94%A8%E7%9A%84GDI%E6%8E%A5%E5%8F%A3%EF%BC%8C%E5%AF%BC%E8%87%B4%E6%98%BE%E7%A4%BA%E5%BF%BD%E7%95%A5%E6%8D%A2%E8%A1%8C%E7%AC%A6%E6%88%96%E4%B8%8D%E5%A4%84%E7%90%86%E6%8D%A2%E8%A1%8C%E7%AC%A6%E3%80%82"><span class="nav-number">1.1.2.</span> <span class="nav-text">在对文本进行换行显示的时候，有一点特别需要注意，就是在显示窗体的xml文件设置中，需要将设置为透明属性，即 bktrans&#x3D;”true”，这一属性在Duilib中默认是false的，所以必须在xml中显示的设置该属性。如果 bktrans&#x3D;”false”，那么Label中的文本就不会换行显示，而是只显示一行。这个问题的具体原因不太清楚，推测 bktrans 属性的设置会更改绘图引擎调用的GDI接口，导致显示忽略换行符或不处理换行符。</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">lee</p>
  <div class="site-description" itemprop="description">记录备份</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">10</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">12</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wslmh" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wslmh" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:wslmh@outlook.com" title="E-Mail → mailto:wslmh@outlook.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2020/01/10/Duilib-%E8%87%AA%E5%8A%A8%E6%8D%A2%E8%A1%8C/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="lee">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Youan">
      <meta itemprop="description" content="记录备份">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="duilib 控件文字自动换行 | Youan">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          duilib 控件文字自动换行
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-01-10 00:00:00" itemprop="dateCreated datePublished" datetime="2020-01-10T00:00:00+08:00">2020-01-10</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-01-04 20:18:12" itemprop="dateModified" datetime="2023-01-04T20:18:12+08:00">2023-01-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/duilib/" itemprop="url" rel="index"><span itemprop="name">duilib</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Duilib-自动换行的处理"><a href="#Duilib-自动换行的处理" class="headerlink" title="Duilib-自动换行的处理"></a>Duilib-自动换行的处理</h1><p>Duilib中自动没有自动换行的属性，如果需要实现自动换行，就需要在代码中计算总字体的长度，根据需要添加‘\n’换行符进行换行。 </p>
<span id="more"></span>


<h2 id="例子-–-弹框提示中的文本，自动换行的处理"><a href="#例子-–-弹框提示中的文本，自动换行的处理" class="headerlink" title="例子 – 弹框提示中的文本，自动换行的处理"></a>例子 – 弹框提示中的文本，自动换行的处理</h2><p>自定义个性化的弹窗提示框，提示文本在Label中显示。布局根据实际需要进行布局，这里说明一下计算文本长度，以及换行中需要注意的地方。</p>
<ul>
<li><h3 id="具体实现计算文本长度，并换行显示"><a href="#具体实现计算文本长度，并换行显示" class="headerlink" title="具体实现计算文本长度，并换行显示"></a>具体实现计算文本长度，并换行显示</h3>计算字体的长度可以先用Duilib的Frame获取当前的字体大小，然后根据字体，利用系统API计算文本长度。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">// frame是程序中显示弹框的Duilib窗体</span><br><span class="line">HDC hDc = ::GetDC(frame.GetHWND());</span><br><span class="line">// iFontIndex 的值是xml中设置的显示字体索引值</span><br><span class="line">HFONT hFont = frame.m_PaintManage.GetFont(iFontIndex);</span><br><span class="line">::SelectObject(hDc, hFont);</span><br><span class="line"></span><br><span class="line">CString strNewText;</span><br><span class="line">for(int i=1; i&lt;=strText.GetLength(); i++)</span><br><span class="line">&#123;</span><br><span class="line">    CString strTemp = strText.Left(i);</span><br><span class="line">    SIZE size;</span><br><span class="line">    GetTextExtentPoint32(hDc, strText, strTemp.GetLength(), &amp;size);</span><br><span class="line">    if (size.cx &gt; 350)</span><br><span class="line">    &#123;</span><br><span class="line">        bNewLine = true;</span><br><span class="line"></span><br><span class="line">        strNewText += strTemp + _T(&quot;\n&quot;);</span><br><span class="line">        strText = strText.Right(strText.GetLength()-i);</span><br><span class="line"></span><br><span class="line">        i = 1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">strNewText += strText;</span><br><span class="line">::ReleaseDC(frame.GetHWND(), hDc);</span><br><span class="line"></span><br><span class="line">// m_pLabelTip是窗体中显示文本的Label</span><br><span class="line">if (bNewLine)</span><br><span class="line">    frame.m_pLabelTip-&gt;SetAttribute(_T(&quot;align&quot;), _T(&quot;left&quot;));</span><br><span class="line">else</span><br><span class="line">    frame.m_pLabelTip-&gt;SetAttribute(_T(&quot;align&quot;), _T(&quot;center&quot;));</span><br><span class="line"></span><br><span class="line">frame.m_pLabelTip-&gt;SetText(strNewText);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>上面的代码，把单行最大长度定义为350像素，这个值由具体需求而定。同时也根据是否需要换行显示而动态修改了Label中文本的对其方式。</p>
<ul>
<li><h3 id="在对文本进行换行显示的时候，有一点特别需要注意，就是在显示窗体的xml文件设置中，需要将设置为透明属性，即-bktrans-x3D-”true”，这一属性在Duilib中默认是false的，所以必须在xml中显示的设置该属性。如果-bktrans-x3D-”false”，那么Label中的文本就不会换行显示，而是只显示一行。这个问题的具体原因不太清楚，推测-bktrans-属性的设置会更改绘图引擎调用的GDI接口，导致显示忽略换行符或不处理换行符。"><a href="#在对文本进行换行显示的时候，有一点特别需要注意，就是在显示窗体的xml文件设置中，需要将设置为透明属性，即-bktrans-x3D-”true”，这一属性在Duilib中默认是false的，所以必须在xml中显示的设置该属性。如果-bktrans-x3D-”false”，那么Label中的文本就不会换行显示，而是只显示一行。这个问题的具体原因不太清楚，推测-bktrans-属性的设置会更改绘图引擎调用的GDI接口，导致显示忽略换行符或不处理换行符。" class="headerlink" title="在对文本进行换行显示的时候，有一点特别需要注意，就是在显示窗体的xml文件设置中，需要将设置为透明属性，即 bktrans&#x3D;”true”，这一属性在Duilib中默认是false的，所以必须在xml中显示的设置该属性。如果 bktrans&#x3D;”false”，那么Label中的文本就不会换行显示，而是只显示一行。这个问题的具体原因不太清楚，推测 bktrans 属性的设置会更改绘图引擎调用的GDI接口，导致显示忽略换行符或不处理换行符。"></a>在对文本进行换行显示的时候，有一点特别需要注意，就是在显示窗体的xml文件设置中，需要将<Windows>设置为透明属性，即 bktrans&#x3D;”true”，这一属性在Duilib中默认是false的，所以必须在xml中显示的设置该属性。如果 bktrans&#x3D;”false”，那么Label中的文本就不会换行显示，而是只显示一行。这个问题的具体原因不太清楚，推测 bktrans 属性的设置会更改绘图引擎调用的GDI接口，导致显示忽略换行符或不处理换行符。</h3></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/duilib/" rel="tag"># duilib</a>
              <a href="/tags/%E6%8E%A7%E4%BB%B6%E6%96%87%E5%AD%97%E6%8D%A2%E8%A1%8C/" rel="tag"># 控件文字换行</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2019/09/25/Windows-kernel/" rel="prev" title="Windows核心编程-内核对象">
                  <i class="fa fa-chevron-left"></i> Windows核心编程-内核对象
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2020/11/13/Windows%E5%B1%8F%E5%B9%95%E5%88%86%E8%BE%A8%E7%8E%87%E4%B8%8EDPI%E7%9A%84%E8%8E%B7%E5%8F%96/" rel="next" title="Windows屏幕分辨率与DPI的获取">
                  Windows屏幕分辨率与DPI的获取 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">lee</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
